<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Jialeo\LaravelSchemaExtend\Schema;

class AddDateToCheckStreams extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('check_streams', function (Blueprint $table) {
            $table->unsignedInteger('date')->default(0)->after('tradeTime');
            $table->index('date', 'idx_date');
        });

        \App\Models\Check\CheckStreamModel::query()->get()->each(function($item) {
            $item->date = (int)date('Ymd', $item->tradeTime);
            $item->save();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('check_streams', function (Blueprint $table) {
            $table->dropColumn('date');
        });
    }
}
